<?php
/**
  * tframe
  *
  * 处理上传文件
  * @copyright Copyright (c) 2012 - 2014, huxiaomin
  * @author huxiaomin    2012-7-14  
  * @since		Version 1.0
  *
  */


class TUpload{

    function __construct(){
    
    }
    
    /**
     * array(
     *      'file' => $_FILES['file'],
     *      'to' => 'dir',
     *      'ext'=> 'doc|xls|ppt|avi|txt|gif|jpg|jpeg|bmp|png',
     *      'max_size'=> '5',//字节
     *      'name'=>'',//默认上传文件名
     *      'cover'=> 1,//默认覆盖
     * )
     * @param $save
     * @return array
     */
    function save($save = array(
					           'file' => '',//$_FILES['file'],
					           'to' => '',//dir
					           'ext'=> '',//pdf,txt,img
					           'max_size'=> '',//字节
					           'name'=>'',//默认上传文件名
					           'cover'=> 1,//默认覆盖
					      )){
        $file = $save['file'];
        if ($file['error']){
            $error =$this->fileError($file['error']);
            return $this->out(0,$error);
        }
		if (!file_exists($file['tmp_name'])){
            return $this->out(0,'up file not exists');
        }
        if (!empty($save['max_size']) && $save['max_size'] < $file['size']){
            $error = 'max_size false';
        }
        $file_pathinfo = pathinfo($file['name']);
        if (empty($save['ext'])){
        	$save['ext'] = 'doc|xls|ppt|avi|txt|gif|jpg|jpeg|bmp|png';
        }
		if (!in_array($file_pathinfo['extension'],explode('|',$save['ext']))){
            $error = 'ext false';
            return $this->out(0,$error);
        }
        if (empty($save['name'])){
            $save['name'] = $file_pathinfo['filename'];
        }
        if (empty($save['to'])){
            $save['to'] = '.';
        }
        if (!$this->to($save['to'])){
            $error = 'to false';
            return $this->out(0,$error);
        }
        if (strlen($save['to']) && !in_array(substr($save['to'],-1),array('/','\\'))){
            $save['to'] = $save['to'].'/';
        }
        $to_file = $save['to'].$save['name'].'.'.$file_pathinfo['extension'];
        if (key_exists('cover',$save) 
            && !$save['cover'] 
            && file_exists($to_file)){
            $error = 'cover false';
            return $this->out(0,$error);  
            //TODO rand name  
        }
        //兼容window
        if (DIRECTORY_SEPARATOR != '/'){
            $move_to_file = $save['to'].
                mb_convert_encoding($save['name'],'gbk','utf-8').
                '.'.$file_pathinfo['extension'];
        }else {
            $move_to_file = $to_file;
        }
        if (!$this->move($file['tmp_name'], $move_to_file)){
            $error = 'save file false';
            return $this->out(0,$error);
        }
        
        return $this->out('1','success',array(
                    'ext'=>$file_pathinfo['extension'],
                    'name'=>$save['name'],
                    'to'=>$save['to'],
                    'file'=>$to_file,
                    'size'=>$file['size'],
                ));
    }
    
    /**
     * 获取error获取错误原因
     * @param int $num
     */
    function fileError($num){
        return $string='';
    }
    
    /**
     * 检查to
     * @param $to
     */
    function to($dir){
        if (!is_dir($dir)){
           $mk = mkdir($dir,0777,1);
           if (!$mk){
              return false;
           }
        }
        return true;
    }
    
    /**
     * 转移到指定位置
     * @param $from
     * @param $to
     */
    function move($from, $to){
       if (!copy($from,$to)){
            return false;
       }
       return true;
    }
    
    function out($success=0, $msg='', $data=array()){
        return array(
            'success' => !empty($success),
            'msg' => $msg,
            'data' => $data,
        );
    }

}
    